package org.aeon.colorfulplanebattle.flying.entity;

import org.aeon.colorfulplanebattle.constant.BulletE;
import org.aeon.colorfulplanebattle.constant.DirectionE;
import org.aeon.colorfulplanebattle.flying.Utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;
import java.util.UUID;

import static org.aeon.colorfulplanebattle.constant.GameBasisConstant.*;

/**
 * @author: MissingAeon
 * @date: 2024/1/9 12:49
 * @description: 子弹
 */
public class Bullet extends FlyObject implements Cloneable {
    private static final HashMap<String, BufferedImage> pictures = new HashMap<>(); // 子弹图片
    private static final Properties prop; // 这里保存配置信息

    static {
        prop = Utils.getProp(BULLET);
        try {
            for (BulletE bulletE : BulletE.values()) {
                InputStream resource = Ship.class.getClassLoader().getResourceAsStream("img/zidan/zidan_" + bulletE + ".png");
                if (resource == null) break;
                pictures.put(bulletE.name(), ImageIO.read(resource));
            }
        } catch (IOException ignored) {
        }
    }

    private BufferedImage picture;

    public Bullet(int camp, int speed) {
        super();
        this.camp = camp;
        this.speed = speed;

        Utils.stuffing(prop, this);

        // 玩家子弹速度为负，反向
        if (camp == FRIENDLY_CAMP) {
            this.speed = -this.speed;
        }
        if (camp != FRIENDLY_CAMP) {
            picture = pictures.get(BulletE.RED.name());
            picture = Utils.flippedImage(picture);
        } else {
            picture = pictures.get(BulletE.BLUE.name());
        }
    }


    /**
     * 子弹移动
     *
     * @param direction 方向
     * @return
     */
    @Override
    public boolean move(DirectionE direction) {
        if (y > BATTLEFIELD_HEIGHT + height + 5 || y < -height - 5) return false;
        y += speed;
        return true;
    }

    @Override
    public void draw(Graphics g) {
        g.drawImage(picture, x, y, width, height, null);
        g.setColor(Color.green);
        g.drawRect(x + (width - touchWidth) / 2, y + (height - touchHeight) / 2, touchWidth, touchHeight);
    }

    @Override
    public String toString() {
        return "Bullet{" +
                "picture=" + picture +
                ", x=" + x +
                ", y=" + y +
                ", width=" + width +
                ", height=" + height +
                ", touchWidth=" + touchWidth +
                ", touchHeight=" + touchHeight +
                ", camp=" + camp +
                ", color=" + color +
                ", speed=" + speed +
                ", isDestroy=" + isDestroy +
                '}';
    }

    @Override
    public Bullet clone() {
        try {
            Bullet clone = (Bullet) super.clone();
            clone.flyObjectId = UUID.randomUUID();
            return clone;
        } catch (CloneNotSupportedException e) {
            throw new AssertionError();
        }
    }

    @Override
    public void setColor(String color) {
        this.color = color;
        this.picture = pictures.get(this.getColor());
    }
}
